<?php

Class Application_Model_UserModel extends Zend_Db_Table_Abstract {

    protected $_name = 'user_mgmt';
    protected $_fields = 'id,fname,lname,email,phone,cell,role,department,status,lastlogin';
    public $adapter;

    function init() {
        $this->adapter = $this->getAdapter();
    }

    public function _getAuthAdapter() {

        $authAdapter = new Zend_Auth_Adapter_DbTable($this->adapter);
        $authAdapter->setTableName('user_mgmt')
                ->setIdentityColumn('email')
                ->setCredentialColumn('password');

        return $authAdapter;
    }

    function getAllUsers($request) {
        $userDetails = Zend_Auth::getInstance()->getIdentity();
        $select = $this->select()
                ->from($this->_name, array('id', 'fname', 'lname', 'email', 'phone', 'cell', 'role', 'department', 'status', 'lastlogin'));
        if ($userDetails->role == 'admin') {
            $select->where("department = '{$userDetails->department}'");
        }
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function save($data) {
        if (isset($data['id'])) {
            return $this->update($data, "id = $data[id]");
        } else {
            return $this->insert($data);
        }
    }

    function getUser($field, $value) {
        //   echo "select $this->_fields from $this->_name where $field = '$value'";exit;
        return $this->adapter->fetchRow("select $this->_fields from $this->_name where $field = '$value'");
    }

    function getAllSwitchUserNames($switchId) {
        $select = $this->select()
                ->from($this->_name, array('fname'))
                ->joinInner('switch_users', "switch_users.user_id = user_mgmt.id", array(''))
                ->WHERE("switch_users.switch_id = '$switchId'");
        return $this->adapter->fetchCol($select);
    }

    function getAllStreetUserNames($switchId) {
        $select = $this->select()
                ->from($this->_name, array('fname'))
                ->joinInner('street_team', "street_team.user_id = user_mgmt.id", array(''))
                ->WHERE("street_team.switch_id = '$switchId'");
        return $this->adapter->fetchCol($select);
    }

    function getAllDepartmentUsers($department) {
        $select = $this->select()
                ->from($this->_name, array('id', 'fname'))
                ->WHERE("department = '$department' AND status = 'active'");
        return $this->adapter->fetchAll($select);
    }
    
    function getAllHouseUserNames($switchId) {
        $select = $this->select()
                ->from($this->_name, array('fname'))
                ->joinInner('house_team', "house_team.user_id = user_mgmt.id", array(''))
                ->WHERE("house_team.switch_id = '$switchId'");
        return $this->adapter->fetchCol($select);
    }
    
    function getAllInstallationUserNames($switchId) {
        $select = $this->select()
                ->from($this->_name, array('fname'))
                ->joinInner('install_team', "install_team.user_id = user_mgmt.id", array(''))
                ->WHERE("install_team.switch_id = '$switchId'");
        return $this->adapter->fetchCol($select);
    }

}